//
//  CTMediator+CYNetwork.m
//  CYNetworkComponent_Example
//
//  Created by lc4y on 2018/2/15.
//  Copyright © 2018年 lc4y. All rights reserved.
//

#import "CTMediator+CYNetwork.h"
#import <YTKNetwork/YTKNetwork.h>

// 模块名称
NSString * const kCYNetworkTarget = @"Network";
// 网络请求成功的block
NSString * const kCYNetworkRequest = @"kCYNetworkRequest";
// 设置服务器URL
NSString * const kCYNetworkServerURL = @"kCYNetworkServerURL";
// 设置公共参数
NSString * const kCYNetworkCommonArgu = @"kCYNetworkCommonArgu";

// GET请求
NSString *const kCYNetworkActionGetDataFromServer = @"getDataFromServer";
// POST请求
NSString *const kCYNetworkActionPostDataFromServer = @"postDataFromServer";
// POST上传图片
NSString *const kCYNetworkActionPostFileToServer = @"postFileToServer";
// 配置服务器地址
NSString *const kCYNetworkActionConfigServerURL = @"configServerURL";
// 配置公共参数
NSString *const kCYNetworkActionConfigCommonArauments = @"configCommonArguments";

@implementation CTMediator (CYNetwork)

- (void)CYNetwork_configServerUrl:(NSString *)serverUrl {
    [self performTarget:kCYNetworkTarget action:kCYNetworkActionConfigServerURL params:@{kCYNetworkServerURL:serverUrl ? serverUrl : @""} shouldCacheTarget:NO];
}

- (void)CYNetwork_configCommonArguments:(NSDictionary *)commonArgument delegate:(id<CYNetworkCommonArgumentsDelegate>)delegate{
    NSDictionary *paramsDict = delegate ? @{kCYNetworkCommonArgu:commonArgument,@"delegate":delegate} : @{kCYNetworkCommonArgu:commonArgument};
    [self performTarget:kCYNetworkTarget action:kCYNetworkActionConfigCommonArauments params:paramsDict
      shouldCacheTarget:NO];
}

- (void)CYNetwork_getDataFromServerWithURL:(NSString *)url params:(NSDictionary *)params complete:(CYNetWorkRequest)requestBlock {
    if( !url || !requestBlock ) {
        return;
    }
    
    params = !params ? @{} : params;
    [self performTarget:kCYNetworkTarget action:kCYNetworkActionGetDataFromServer params:@{kCYNetworkRequest:requestBlock,@"URL":url,@"params":params}
      shouldCacheTarget:NO];
}

- (void)CYNetwork_postDataFromServerWithURL:(NSString *)url params:(NSDictionary *)params complete:(CYNetWorkRequest)requestBlock {
    if( !url || !requestBlock ) {
        return;
    }
    
    params = !params ? @{} : params;
    [self performTarget:kCYNetworkTarget action:kCYNetworkActionPostDataFromServer params:@{kCYNetworkRequest:requestBlock,@"URL":url,@"params":params}
      shouldCacheTarget:NO];
}

- (void)CYNetwork_postFileToServerWithURL:(NSString *)url params:(NSDictionary *)params fileData:(NSData *)fData fileType:(NSString *)fType name:(NSString *)serverName complete:(CYNetWorkRequest)requestBlock {
    if( !url || !requestBlock || !fData || !fType || !serverName) {
        return;
    }
    
    params = !params ? @{} : params;
    [self performTarget:kCYNetworkTarget action:kCYNetworkActionPostFileToServer params:@{kCYNetworkRequest:requestBlock,@"URL":url,@"params":params,@"fileData":fData,@"fileType":fType,@"name":serverName}
      shouldCacheTarget:NO];
}


@end
